package com.example.j20240313springmvc.controller;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RequestMapping("/Response")
@Controller
public class ResponseController {

    //返回静态页面
    @RequestMapping("/response1")
    public String response1() {
        return "/response1.html";
    }

    //返回HTML代码片段
    @ResponseBody
    @RequestMapping("/response2")
    public String response2() {
        return "<h1>这是一个HTML代码片段<h1>";
    }

    //返回JSON
    @ResponseBody
    @RequestMapping("/response3")
    public Map<String, String> response3() {
        Map<String, String> map = new HashMap<>();
        map.put("k1", "v1");
        map.put("k2", "v2");
        map.put("k3", "v3");
        map.put("k4", "v4");
        return map;
    }
    @ResponseBody
    @RequestMapping("/response4")
    public String response4(HttpServletResponse response) {
        response.setStatus(418);
        return "设置状态码成功";
    }
    @ResponseBody
    @RequestMapping(value = "/response5" , method = RequestMethod.GET)
    public String setMethod() {
        return "接收请求成功";
    }
    @ResponseBody
    @RequestMapping(value = "/response6", produces = "application/json")
    public String setContentType() {
        return "\"Key\", 200";
    }
}
